﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.SessionState;

namespace StudentManagerPro.Handlers
{
    /// <summary>
    /// LoginHandler 的摘要说明
    /// </summary>
    public class LoginHandler : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //1.获取用户名与密码
            var name = context.Request.Params["username"].ToString();
            var pwd = context.Request.Params["pwd"].ToString();          
            SysAdmin admin = new SysAdmin
            {
                AdminName = name,
                LoginPwd = pwd,
            };
            //2.调用后台，判断用户名与密码是否正确
            try
            {
                admin = new SysAdminService().AdminLogin(admin);
                if (admin == null)
                {
                    //用户名密码错误则返回1
                    context.Response.Write(0);
                }
                else
                {
                    //用户名密码争取则将用户信息存入到session中，翻译1
                    Thread.Sleep(3000);
                    context.Session["CurrentUser"] = admin;//需要基础IRequiresSessionState接口
                    context.Response.Write(1);
                }
            }
            catch (Exception ex)
            {
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}